package com.zwy.space.controller.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.space.domain.Blog;
import com.zwy.space.domain.BlogType;
import com.zwy.space.domain.Images;
import com.zwy.space.domain.User;
import com.zwy.space.queryVo.BlogDetailVo;
import com.zwy.space.queryVo.BlogVo;
import com.zwy.space.queryVo.UserVo;
import com.zwy.space.queryVo.admin.SearchBlogVo;
import com.zwy.space.queryVo.admin.ShowBlog;
import com.zwy.space.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin/blogs")
public class AdminBlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping
    public String findAllBlogs(Model model, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<BlogVo> blogVoList=blogService.findAllBlogs();
        List<BlogType>blogTypeList=blogService.findAllTypes();

        PageInfo<BlogVo> pageInfo = new PageInfo<>(blogVoList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",blogTypeList);

        return "admin/blogs";
    }

    //    搜索博客
    @PostMapping("/search")
    public String search(SearchBlogVo searchBlog, Model model,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        List<BlogVo> blogBySearch = blogService.findBlogsBySerach(searchBlog);
        PageHelper.startPage(pageNum, 10);
        PageInfo<BlogVo> pageInfo = new PageInfo<>(blogBySearch);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs :: blogList";
    }

    @RequestMapping("/input")
    public String InputBlog(Model model){
        model.addAttribute("types",blogService.findAllTypes());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }



    //    博客新增
    @PostMapping
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){

        UserVo user=(UserVo)session.getAttribute("user");
//        //设置blog的type
//        blog.setType(typeService.getType(blog.getType().getId()));
        //设置blog中typeId属性
//        blog.setTypeId(blog.getType().getId());
        blog.setTypeId(blog.getTypeId());
        //设置用户id
        blog.setUserId(user.getId());

        int b = blogService.saveBlog(blog);

        if(b == 0){
            attributes.addFlashAttribute("message", "新增失败");
        }else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/blogs";
    }


    @RequestMapping("/{blogId}/input")
    public String editBlog(@PathVariable int blogId,Model model){
        Blog blogById = blogService.editBlogById(blogId);
        List<BlogType> allType = blogService.findAllTypes();
        model.addAttribute("blog", blogById);
        model.addAttribute("types", allType);
        return "admin/blogs-input";
    }

    //    编辑修改文章
    @PostMapping("/{id}")
    public String editPost(@Valid ShowBlog showBlog, RedirectAttributes attributes) {
        int b = blogService.updateBlog(showBlog);
        if(b ==0){
            attributes.addFlashAttribute("message", "修改失败");
        }else {
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin/blogs";
    }

    //    删除文章
    @GetMapping("/{id}/delete")
    public String delete(@PathVariable int id, RedirectAttributes attributes) {
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }


}
